# @Time : 2024/12/20 10:31
# @Author : ZHUYI
# @File : spider_juejin
import shutil
from webdriver_manager.chrome import ChromeDriverManager
from selenium import webdriver
from selenium.webdriver.chrome.service import Service as ChromeService
from selenium.webdriver.common.by import By
import time


def spider():
    # 1.使用ChromeDriverManager安装ChromeDriver，并返回驱动程序的路径
    driver_path = ChromeDriverManager().install()
    # 打印驱动程序路径
    print(driver_path)
    new_path = 'D:/soft/selenium/'
    # 2.把下载的驱动文件，复制到指定位置
    shutil.copy(driver_path, new_path)
    # 3.验证一下安装好的驱动，是否可用
    driver = webdriver.Chrome(service=ChromeService(new_path + 'chromedriver.exe'))
    print("开始爬虫---->")
    driver.get("https://juejin.cn/")
    time.sleep(2)
    driver.maximize_window()
    driver.find_element(By.XPATH, '//*[@id="juejin"]/div[1]/div[1]/header/div/nav/ul/li/ul/li[4]/a').click()
    time.sleep(2)
    driver.find_element(By.XPATH,
                        '//*[@id="juejin"]/div[1]/div/div/div/div[1]/div[1]/div/div[2]/div[2]/div/div/a[3]/span').click()
    time.sleep(2)
    driver.find_element(By.XPATH,
                        '//*[@id="juejin"]/div[1]/div/div/div/div[1]/div[1]/div/div[3]/div[1]/div/div/a[3]').click()
    time.sleep(3)
    driver.find_element(By.XPATH,
                        '//*[@id="juejin"]/div[1]/div/div/div/div[1]/div[1]/div/div[3]/div[2]/div/div/a[1]').click()
    window_handles = driver.window_handles
    driver.switch_to.window(window_handles[1])  # 切换窗口
    driver.find_element(By.XPATH,
                        '//*[@id="juejin"]/div[1]/div/main/div[1]/div[2]/div[1]/div/div[1]/div/div[3]/span').click()
    time.sleep(2)
    comment_list = driver.find_elements(By.XPATH,
                                        '//div[@class="content" and not(ancestor::div[@class="comment-reply-wrapper"])]')
    with open('csv/juejin_comment.csv', 'w', encoding='utf-8', newline='') as file:
        for c in comment_list:
            print(c.text)
            file.write(c.text + '\n')
    file.close()
    time.sleep(2)
    driver.close()
    print("爬虫结束---->")
